package c.x.jy.service.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import c.x.jy.common.utils.GetClassinfo;
import c.x.jy.common.utils.SysUtils;
import c.x.jy.dao.entity.SysUserEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import c.x.jy.common.utils.PageUtils;
import c.x.jy.common.utils.Query;
import c.x.jy.dao.dao.TeacherDao;
import c.x.jy.dao.dto.TeacherEntityDto;
import c.x.jy.dao.entity.TeacherEntity;
import c.x.jy.service.service.TeacherService;


@Service("teacherService")
public class TeacherServiceImpl extends ServiceImpl<TeacherDao, TeacherEntity> implements TeacherService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        	QueryWrapper<TeacherEntity> query= 	 new QueryWrapper<TeacherEntity>() ;
    	if(params.get("searchKey")!=null&&params.get("searchValue")!=null&&!StringUtils.isEmpty(params.get("searchKey").toString())&&!StringUtils.isEmpty(params.get("searchValue").toString()))
    	    query.eq(  "t."+params.get("searchKey").toString(), params.get("searchValue"));
        if(params.get("businessId")!=null) 	query.eq("t.business_id", params.get("businessId"));
        IPage<TeacherEntityDto> page = this.getBaseMapper().dtoPage(
                new Query<TeacherEntity>().getPage(params),
                 query
        );

        return new PageUtils(page);
    }

    @Override
    public TeacherEntity getByMemberId(Long memberId) {
        TeacherEntity teacherEntity=null;
        if(memberId!=null){
            teacherEntity = baseMapper.selectOne(
                    new QueryWrapper<TeacherEntity>().lambda().eq(TeacherEntity::getMemberId, memberId).last(" limit 1 "));
        }

        return teacherEntity;
    }

    @Override
    public TeacherEntityDto selectById(Long id) {
        return baseMapper.selectDtoById(id);
    }

//    @Override
//    public TeacherEntityDto getDetailByID(Long id) {
//        QueryWrapper<TeacherEntity> query= 	 new QueryWrapper<TeacherEntity>() ;
//        if(id!=null) 	query.eq("id", id);
//        return this.baseMapper.selectListDto(query);
//    }


    @Override

    public  List<TeacherEntityDto> hotTeachers() {
        QueryWrapper<TeacherEntity> query= 	 new QueryWrapper<TeacherEntity>() ;
//        query .eq("t.hot",1);
        query.lambda().eq(TeacherEntity::getHot,1);
        List<TeacherEntityDto> list = this.getBaseMapper().selectListDto(query);

        return list;
    }

    @Override
    public List<TeacherEntityDto>  selectTeacherInfoByServiceId(Long serviceId,Integer sortby,String name) {
        QueryWrapper<TeacherEntity> query= 	 new QueryWrapper<TeacherEntity>() ;
        if(serviceId!=null)query.eq("ts.service_id",serviceId);
        if(sortby!=null)switch (sortby){
            case 1 :
                query.orderByDesc("t.create_date");
                break;
            case 2:
                query.orderByDesc("t.evaluate_score");
                break;
            case 3:
                query.orderByDesc("t.evaluate_number");
                break;
            default:
                query.orderByDesc("t.create_date");
                break;
        }
        if(!StringUtils.isEmpty(name)){
            query.like("ts.description ",  "%"+name+"%" );
        }
        List<TeacherEntityDto> list = this.baseMapper.selectTeacherInfoByServiceId(query);
        return list;
    }

    @Override
    public void passById(Long id, SysUserEntity checkUser) {
        TeacherEntity teacherEntity = getBaseMapper().selectById(id);
        teacherEntity.setCheckId(checkUser.getUserId());
        teacherEntity.setStatus(2);
        teacherEntity.setCheckDate(new Date());
        getBaseMapper().updateById(teacherEntity);
    }

    @Override
    public void rejectById(Long id, SysUserEntity checkUser) {
        TeacherEntity teacherEntity = getBaseMapper().selectById(id);
        teacherEntity.setCheckId(checkUser.getUserId());
        teacherEntity.setStatus(-1);
        teacherEntity.setCheckDate(new Date());
        getBaseMapper().updateById(teacherEntity);
    }


	@Override
	public List<TeacherEntity> selectTeachersByJoinQuickAsk() {
 
		return   getBaseMapper().selectTeachersByJoinQuickAsk();
	}

    @Override
    public PageUtils selectlist(Map<String, Object> params) {
        QueryWrapper<TeacherEntity> query= 	 new QueryWrapper<TeacherEntity>() ;

         if(params.containsKey("teacherid")){
             query.eq("t.id",params.get("teacherid"));
         }
        if(params.containsKey("serviceId")){
            query.eq("ts.service_id",params.get("serviceId"));
        }
        IPage<TeacherEntityDto> page = this.getBaseMapper().selectlist(
                new Query<TeacherEntity>().getPage(params),
                query
        );

        return new PageUtils(page);
    }
}
